package com.example.flutter_channel_plugin;

import android.util.Log;

import androidx.annotation.NonNull;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;

/**
 * FlutterChannelPlugin
 */
public class FlutterChannelPlugin implements FlutterPlugin, MethodCallHandler {
    /// The MethodChannel that will the communication between Flutter and native Android
    ///
    /// This local reference serves to register the plugin with the Flutter Engine and unregister it
    /// when the Flutter Engine is detached from the Activity
    private MethodChannel channel;

    @Override
    public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
        Log.d("FlutterChannelPlugin", "onAttachedToEngine:" + Log.getStackTraceString(new Throwable()));
        channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "flutter_channel_plugin");
        channel.setMethodCallHandler(this);
    }

    @Override
    public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
        Log.d("FlutterChannelPlugin", "" + call + " " + Thread.currentThread() + " " + Log.getStackTraceString(new Throwable()));
        Log.d("FlutterChannelPlugin", "method:" + call.method);
        Log.d("FlutterChannelPlugin", "arguments:" + call.arguments);
        if (call.method.equals("getPlatformVersion")) {
            result.success("Android " + android.os.Build.VERSION.RELEASE);
        } else {
            //result.notImplemented();
            //result.error("404", "沒有權限", "沒有存儲權限");
            result.success("Android to Flutter:" + call.arguments);
        }
    }

    @Override
    public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
        channel.setMethodCallHandler(null);
    }
}
